package com.dhcc.core.modules.system.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.entity.Job;
import com.dhcc.core.modules.system.service.IJobService;

/**
 * 定时任务
 * 
 * @ClassName: JobController
 * @Description: TODO
 * @author: cyf
 * @date: 2018年1月2日 上午10:19:08
 */
@Controller
@RequestMapping("${sys.backendPath}/job")
public class JobController extends BaseController {

    private String PREFIX = "/backend/system/job/";

    @Autowired
    private IJobService jobService;

    /**
     * 跳转到定时任务列表首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "job.html";
    }

    /**
     * 跳转到添加定时任务
     */
    @RequestMapping("/to_add")
    public String jobAdd() {
        return PREFIX + "job_add.html";
    }

    /**
     * 跳转到修改定时任务
     */
    @RequestMapping("/to_update/{jobId}")
    public String jobUpdate(@PathVariable Long jobId, Model model) {
        Job job = this.jobService.selectById(jobId);
        model.addAttribute("job", job);
        return PREFIX + "job_edit.html";
    }

    /**
     * 获取定时任务列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        Page<Job> page = new PageFactory<Job>().defaultPage();
        List<Job> result = this.jobService.list(page, params);
        page.setRecords((List<Job>) new EmptyWarpper(result).warp());
        return R.packForBT(page);
    }

    /**
     * 新增定时任务
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Job job) {
        if (CommonUtil.isOneEmpty(job)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        jobService.save(job);
        return R.ok();
    }

    /**
     * 删除定时任务
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Long jobId) {
        jobService.delete(jobId);
        return R.ok();
    }

    /**
     * 修改定时任务
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Job job) {
        if (CommonUtil.isOneEmpty(job)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        Job old = this.jobService.selectById(job.getId());
        old.setBeanName(job.getBeanName());
        old.setMethodName(job.getMethodName());
        old.setParams(job.getParams());
        old.setCronExpression(job.getCronExpression());
        old.setStatus(job.getStatus());
        old.setDescription(job.getDescription());
        jobService.update(old);
        return R.ok();
    }

    /**
     * 立即执行任务
     */
    @RequestMapping("/run")
    @ResponseBody
    public R run(@RequestParam Long jobId) {
        jobService.run(jobId);
        return R.ok();
    }

    /**
     * 暂停定时任务
     */
    @RequestMapping("/pause")
    @ResponseBody
    public R pause(@RequestParam Long jobId) {
        jobService.pause(jobId);
        return R.ok();
    }

    /**
     * 恢复定时任务
     */
    @RequestMapping("/resume")
    @ResponseBody
    public R resume(@RequestParam Long jobId) {
        jobService.resume(jobId);
        return R.ok();
    }
    /**
     * 跳转到编辑cron表达式
     */
    @RequestMapping("/to_cronedit")
    public String cronEdit() {
        return PREFIX + "job_cronedit.html";
    }
}
